package name.matthewgreet.example5.common; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.ValidationAware; import name.matthewgreet.example5.common.MessageStoreInterceptor.StoredMessages; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; /** * <P>If the Struts action implements {@link ValidationAware}, removes errors and messages stored in the session by * {@link MessageStoreInterceptor} and injects it into the action. It is usually part of the aflViewStack interceptor stack.</P> * * * <H3>Interceptor parameters</H3> * <DL> * <DT>disabled</DT><DD>If true, all processing is disabled. This is useful for standalone popup windows, especially * self-refreshing ones, that never display messages. Defaults to false.</DD> * </DL> * * <H3>Extending the interceptor</H3> * <P>This interceptor cannot be usefully extended.</P> * * <H3>Example code</H3> * <PRE> * @InterceptorRefs({ * @InterceptorRef(value="messageRetrieve"), * @InterceptorRef(value="basicStack") *}) * </PRE> */ public class MessageRetrieveInterceptor extends AbstractInterceptor { private static final long serialVersionUID = -6513506646328167140L; private Logger LOG = LogManager.getLogger(MessageRetrieveInterceptor.class); private boolean disabled; public MessageRetrieveInterceptor() { // Empty } public boolean getDisabled() { return disabled; } public void setDisabled(boolean value) { disabled = value; } @Override public String intercept(ActionInvocation invocation) throws Exception { StoredMessages storedMessages; ValidationAware validationAware; Object rawObject; if (!disabled && invocation.getAction() instanceof ValidationAware) { rawObject = ActionContext.getContext().getSession().get(MessageStoreInterceptor.SESSION_STORED_MESSAGES); if (rawObject != null) { if (rawObject instanceof StoredMessages) { storedMessages = (StoredMessages)rawObject; validationAware = (ValidationAware)invocation.getAction(); validationAware.setActionErrors(storedMessages.getActionErrors()); validationAware.setActionMessages(storedMessages.getActionMessages()); validationAware.setFieldErrors(storedMessages.getFieldErrors()); ActionContext.getContext().getSession().remove(MessageStoreInterceptor.SESSION_STORED_MESSAGES); } else { LOG.warn("Session attribute are not stored messages session attribute=" + MessageStoreInterceptor.SESSION_STORED_MESSAGES + " value=" + rawObject); } } } return invocation.invoke(); } }